package com.nbgs.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbgs.demo.common.Result;
import com.nbgs.demo.controller.form.UserNews;
import com.nbgs.demo.model.Card;
import com.nbgs.demo.model.Consumption;
import com.nbgs.demo.model.Meter;
import com.nbgs.demo.model.User;
import com.nbgs.demo.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;

@RestController
@Slf4j
@CrossOrigin("*")
public class UserManagerController {
  @Autowired
  private UserService userService;
  @Autowired
  private CardService cardService;
  @Autowired
  private MeterService meterService;
  @Autowired
  private ConsumptionService consumptionService;
  @Autowired
  private AreaService areaService;
     //录入用户信息
    @PostMapping("addUser")
    public Result addUser(@RequestBody UserNews userNews){
        log.info("录入单个用户信息，{}",userNews);

        User user= new User();
        user.setIDNum(userNews.getIDNum());
        user.setUserAddress(userNews.getUserAddress());
        user.setUsername(userNews.getUsername());
        user.setUserTel(userNews.getUserTel());
        //将用户信息录入
        userService.addUser(user);
        Meter meter = new Meter();
        meter.setAreaNum(areaService.findNum(userNews.getAreaName()));
        meter.setMeterAddress(userNews.getMeterState());
        meter.setMeterDate(new Date());
        meter.setMeterState("正常");
        //录入用户的水表信息
        meterService.addMeter(meter);
        Card card = new Card();
        card.setAreaNum(areaService.findNum(userNews.getAreaName()));
        card.setCardNum(userNews.getCardNum());
        card.setMeterId(meter.getMeterId());
        card.setUserId(userService.findUserIdByIDNum(user.getIDNum()));
        card.setTypeId(cardService.findTypeIdByType(userNews.getType()));
        //录入用户的水卡信息
        cardService.addCard(card);
        //初始化用户用水详情表
        Calendar calendar = Calendar.getInstance();
        Consumption consumption=new Consumption();
        consumption.setCardId(card.getCardId());
        consumption.setConsumptionStart(0.0);
        consumption.setConsumptionEnd(0.0);
        calendar.setTime(new Date());
        consumption.setEntryDate(new Date());
        consumption.setConsumptionMonth(calendar.get(Calendar.YEAR)+","+(calendar.get(Calendar.MONTH)+1));
        consumption.setWorkerName("初始化");
        consumptionService.InsertUserMessage(consumption);

        return Result.success(userNews);
    }
    //页面展示用户信息
    @GetMapping("/allUserNews")
    //提供用户集合前端展示所有
    public Result findAllUser(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "10")int pageSize
                                 ,@RequestParam String areaName,@RequestParam String type,@RequestParam String meterState,@RequestParam String cardNum){
        log.info("前端展示分页{}{}{}{}{}{}",currPage,pageSize,areaName,type,meterState,cardNum);
        Page<UserNews> pageParm = new Page<>();
        pageParm.setCurrent(currPage);
        pageParm.setSize(pageSize);
        pageParm.setTotal(userService.getTotalUserNews(areaName, type, meterState,cardNum));
        IPage<UserNews> allUserNews = userService.showSumUserNews(pageParm,areaName,type,meterState,cardNum);
        return Result.success(allUserNews);
    }
    //修改用户信息
    @PostMapping("/updateUserNews")
    public Result updateAll(@RequestBody UserNews userNews){
        log.info("修改用户信息{}",userNews);
        //通过身份证找到userId
        System.out.println(userNews);
        int userId = userService.findUserIdByIDNum(userNews.getIDNum());
        //通过身份证找到用户修改。
        User user= new User();
        user.setIDNum(userNews.getIDNum());
        user.setUserId(userId);
        user.setUserTel(userNews.getUserTel());
        userService.updateUser(user);
        //修改水表信息
        Meter meter = new Meter();
        meter.setAreaNum(areaService.findNum(userNews.getAreaName()));
        meter.setMeterId(userNews.getMeterId());
        meter.setMeterState(userNews.getMeterState());
        meter.setMeterAddress(userNews.getUserAddress());
        //修改用户的水表信息
       meterService.updateMeter(meter);
       //修改用户id的水卡信息
        cardService.updateCard(userNews.getMeterId(),userNews.getCardNum());
        return Result.success(userNews);
    }


}
